﻿using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
using System;
using System.Windows.Forms;

namespace MyWindowsService
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [System.STAThread()]
        static void Main()
        {
            try
            {
                System.Uri uri = new Uri(typeof(string).Assembly.CodeBase);
                string RuntimePath = System.IO.Path.GetDirectoryName(uri.LocalPath);
                string strInstallUtilPath = System.IO.Path.Combine(RuntimePath, "InstallUtil.exe");
                foreach (string arg in System.Environment.GetCommandLineArgs())
                {
                    Console.WriteLine(arg);
                    if (arg == "/install")
                    {
                        System.Diagnostics.Process.Start(strInstallUtilPath, "\"" + System.Windows.Forms.Application.ExecutablePath + "\"");
                        return;
                    }
                    else if (arg == "/uninstall")
                    {
                        System.Diagnostics.Process.Start(strInstallUtilPath, "/u \"" + System.Windows.Forms.Application.ExecutablePath + "\"");
                        return;
                    }
                    else if (arg == "/client")
                    {
                        // 启动客户端
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);

                        using (frmClient frm = new frmClient())
                        {
                            Application.Run(frm);
                            //frm.ShowDialog();
                            Util.CloseDBConnection();
                        }
                        return;
                    }
                    else if (arg == "/debug")
                    {
                        MyFileSystemWatcherService service = new MyFileSystemWatcherService();
                        service.StartFileSystemWatching();
                        System.Threading.Thread.Sleep(1000 * 600);
                        return;
                    }
                }
            }
            catch (Exception ext)
            {
                Console.WriteLine(ext.ToString());
                return;
            }
            // 运行服务对象
            ServiceBase.Run(new MyFileSystemWatcherService());
        }
    }
}